package com.yinhe.music.yhmusic.widget;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Handler;
import android.support.v7.widget.AppCompatImageView;
import android.util.AttributeSet;

import com.apkfuns.logutils.LogUtils;
import com.yinhe.music.yhmusic.R;
import com.yinhe.music.yhmusic.application.AppCache;
import com.yinhe.music.yhmusic.utils.BitmapDecodeUtil;


/**
 * 专辑封面
 */
public class AlbumCoverView extends AppCompatImageView {
    private static final long TIME_UPDATE = 50L;
    private static final float DISC_ROTATION_INCREASE = 0.5f;
    private Handler mHandler = new Handler();
    private Bitmap mDiscBitmap;
    private float mDiscRotation = 0.0f;
    private boolean isPlaying = false;


    public AlbumCoverView(Context context) {
        this(context, null);
    }

    public AlbumCoverView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public AlbumCoverView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        try {
            if (AppCache.mBitMapCache.containsKey("mDiscBitmap")) {
                mDiscBitmap = AppCache.mBitMapCache.get("mDiscBitmap");
            } else {
                mDiscBitmap = BitmapDecodeUtil.decodeBitmap(this.getContext(), R.drawable.ic_play_circle);
                AppCache.mBitMapCache.put("mDiscBitmap", mDiscBitmap);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private Rect sRect = new Rect();
    private Rect dRect = new Rect();
    private Paint mPaint = new Paint();

    @Override
    protected void onDraw(Canvas canvas) {
        try {
            super.onDraw(canvas);
        } catch (Exception e) {
            LogUtils.e(e);
        }
        mPaint.setAntiAlias(true);
        mPaint.setFilterBitmap(true);
        sRect.left = 0;
        sRect.right = mDiscBitmap.getWidth();
        sRect.top = 0;
        sRect.bottom = mDiscBitmap.getHeight();

        int margin = getResources().getDimensionPixelOffset(R.dimen.dp_8);
        dRect.left = margin;
        dRect.right = getWidth() - margin;
        dRect.top = margin;
        dRect.bottom = getHeight() - margin;

        canvas.drawBitmap(mDiscBitmap, sRect, dRect, mPaint);
    }

    public void setCoverBitmap(Bitmap bitmap) {
        if (bitmap != null) {
            setImageBitmap(bitmap);
        }
    }

    public void start() {
        if (isPlaying) return;
        isPlaying = true;
        mHandler.removeCallbacks(mRotationRunnable);
        mHandler.postDelayed(mRotationRunnable, TIME_UPDATE);
    }

    public void pause() {
        if (!isPlaying) return;
        isPlaying = false;
        mHandler.removeCallbacks(mRotationRunnable);
    }

    private Runnable mRotationRunnable = new Runnable() {
        @Override
        public void run() {
            if (isPlaying) {
                mDiscRotation += DISC_ROTATION_INCREASE;
                if (mDiscRotation >= 360) {
                    mDiscRotation = 0;
                }
                setRotation(mDiscRotation);
                invalidate();
                mHandler.postDelayed(this, TIME_UPDATE);
            }
        }
    };

}
